package home.main.util;

import home.main.entity.Product;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class XLSReader {
	
	private ArrayList<Product> listProducts;
	private Workbook workbook;
	private String sheetName[];
	private int sheetCount;
	private int rowCount;
	private Sheet sheet;
	private Cell[] cell;
	private Cell xlsCell;
	
	
	public XLSReader(String fileName) throws BiffException, IOException {
		listProducts = new ArrayList<Product>();
		workbook = Workbook.getWorkbook(new File(fileName));
		sheetName = workbook.getSheetNames();
		sheetCount = sheetName.length;
		sheet = workbook.getSheet(0);
		rowCount = sheet.getRows();
		read(9, 4);
	}

	private void read(int start, int sizeRowe){
		
		for (int i = start; i < rowCount; i++) {
			cell = sheet.getRow(i);
			// check row value
			if (cell[1].getContents().toString().isEmpty()) {
				continue;
			}
			Product product = new Product();
			product.setArticle(cell[0].getContents().toString());
			product.setColorNumber(cell[1].getContents().toString());
			product.setColor(cell[2].getContents().toString());
			product.setModel(cell[3].getContents().toString());
			listProducts.add(product);
		}
//		showProductList();
	}
	
	
	
	public void showProductList(){
		for (Product product : listProducts) {
			System.out.println(product);
		}
	}
	
	public ArrayList<Product> getListProducts() {
		return listProducts;
	}


	public void setListProducts(ArrayList<Product> listProducts) {
		this.listProducts = listProducts;
	}

	
	
	
}
